package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.Training;
import com.think.teaching.service.impl.TrainingServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 实训 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "实训管理接口")
public class TrainingController {
	
	private final TrainingServiceImpl trainingService;

	public TrainingController(TrainingServiceImpl trainingService) {
		this.trainingService = trainingService;
	}

	@ApiOperation("添加新的实训")
	@PostMapping("admin/training")
	public Result AddNewQuestionTag(@RequestBody()Training data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			Training training = trainingService.AddTraining(data);
			if (training != null) {
				return Result.success(training);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("更新实训信息")
	@RequestMapping(value = "admin/training",method = RequestMethod.PUT)
	public Result UpdateQuestionTag(@RequestBody()Training data) {
		Result result = data.CheckObjectComplete();
		if (trainingService.getById(data.getId()) == null) {
			return Result.failure(ResultCode.TRAINING_NOT_EXIST);
		}
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			Training training = trainingService.UpdateTraining(data);
			if (training != null) {
				return Result.success(training);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("删除实训")
	@RequestMapping(value = "admin/training/{id}",method = RequestMethod.DELETE)
	public Result DeleteQuestionTag(@PathVariable("id")Integer id) {
		Training training = trainingService.getById(id);
		if (training == null) {
			return Result.failure(ResultCode.TRAINING_NOT_EXIST);
		} else {
			if (trainingService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取实训信息详情")
	@RequestMapping(value = "admin/training/{id}",method = RequestMethod.GET)
	public Result GetQuestionTag(@PathVariable("id")Integer id) {
		Training training = trainingService.getById(id);
		if (training == null) {
			return Result.failure(ResultCode.TRAINING_NOT_EXIST);
		} else {

			return Result.success(training);
		}
	}
	
	@ApiOperation("管理员修改实训状态")
	@RequestMapping(value = "admin/training/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateTrainingState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		Training training = trainingService.getById(id);
		if (training == null) {
			return Result.failure(ResultCode.TRAINING_NOT_EXIST);
		} else {
			try {
				training = trainingService.SetTrainingState(id, state);
				return Result.success(training);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	@ApiOperation("管理员搜索实训列表")
	@RequestMapping(value = "admin/trainings/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListTraining(@PathVariable("page")Integer page,@PathVariable("count")Integer count,
			@RequestParam(required = false,defaultValue = "-1")Integer id) {
		return Result.success(trainingService.ListTraining(page, count, false,id));
	}
	
	@ApiOperation("用户搜索实训列表")
	@RequestMapping(value = "user/trainings/{page}/{count}",method = RequestMethod.GET)
	public Result UserListTraining(@PathVariable("page")Integer page,@PathVariable("count")Integer count,
			@RequestParam(required = false,defaultValue = "-1")Integer id) {
		return Result.success(trainingService.ListTraining(page, count, true,id));
	}
}

